package com.flow.asyncflow.core.utils.dateutils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;

import java.time.LocalDateTime;

public class DateUtil {

    /**
     * 获取时间yyyy-MM-dd HH:mm:ss
     *
     * @param localDateTime 时间
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String htFormat_yyyyMMddHH_mm_ss(LocalDateTime localDateTime) {
        return LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_PATTERN);
    }
    /**
     * 获取时间yyyy-MM-dd HH:mm:ss.SSS
     *
     * @param localDateTime 时间
     * @return yyyy-MM-dd HH:mm:ss.SSS
     */
    public static String htFormat_yyyyMMddHH_mm_ss_SSS(LocalDateTime localDateTime) {
        return LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_MS_PATTERN);
    }
    /**
     * 获取时间yyyy-MM-dd"
     *
     * @param localDateTime 时间
     * @return yyyy-MM-dd"
     */
    public static String htFormat_yyyyMMdd(LocalDateTime localDateTime) {
        return LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATE_PATTERN);
    }
    /**
     * 获取时间yyyy-MM-dd HH:mm
     *
     * @param localDateTime 时间
     * @return yyyy-MM-dd HH:mm
     */
    public static String htFormat_yyyyMMddHHmm(LocalDateTime localDateTime) {
        return LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_MINUTE_PATTERN);
    }
    /**
     * 获取时间HH:mm:ss
     *
     * @param localDateTime 时间
     * @return HH:mm:ss
     */
    public static String htFormat_HH_mm_ss(LocalDateTime localDateTime) {
        return LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_TIME_PATTERN);

    }
    /**
     * 获取时间HH:mm
     *
     * @param localDateTime 时间
     * @return HH:mm
     */
    public static String htFormat_HHmm(LocalDateTime localDateTime) {
        String HH_mm_ss = LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_TIME_PATTERN);
        return HH_mm_ss.substring(0, 5);
    }

    public static LocalDateTime htParse(String localDateTimeStr) {
        String[] dateTimeStr = null;
        if (localDateTimeStr.contains("-") && localDateTimeStr.contains(" ") && localDateTimeStr.contains(":")) {
            dateTimeStr = localDateTimeStr.split(" ");
        }
        if (localDateTimeStr.contains("T")) {
            dateTimeStr = localDateTimeStr.split("T");
        }
        String yearStr = "";
        String monthStr = "";
        String dayStr = "";
        String hourStr = "";
        String minuteStr = "";
        String secondStr = "";
        if (dateTimeStr != null && dateTimeStr.length == 2) {
            String date = dateTimeStr[0];
            String time = dateTimeStr[1];
            String[] dateStr = date.split("-");
            if (dateStr.length == 3) {
                yearStr = dateStr[0];
                monthStr = dateStr[1].length() == 1 ? "0" + dateStr[1] : dateStr[1];
                dayStr = dateStr[2].length() == 1 ? "0" + dateStr[2] : dateStr[2];
            }
            String[] timeStr = time.split(":");
            if (timeStr.length >= 2) {
                hourStr = timeStr[0].length() == 1 ? "0" + timeStr[0] : timeStr[0];
                minuteStr = timeStr[1].length() == 1 ? "0" + timeStr[1] : timeStr[1];
                String second;
                if (timeStr.length == 3) {
                    second = timeStr[2];
                } else {
                    second = "00";
                }
                if (second.length() > 2 && second.contains(".")) {
                    String subSecond;
                    subSecond = second.split("\\.")[0];
                    secondStr = subSecond.length() == 1 ? "0" + subSecond : subSecond;
                } else {
                    secondStr = second.length() == 1 ? "0" + second : second;
                }
            }
            localDateTimeStr = yearStr + "-" + monthStr + "-" + dayStr + "T" + hourStr + ":" + minuteStr + ":" + secondStr;
        }

        return LocalDateTimeUtil.parse(localDateTimeStr);


    }
}
